package com.monkey.luffer.springboot.demo.basis.thread;

import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author kaibing.deng
 * @Date 2024/6/25 17:58
 * @description
 **/
public class TransmittableThreadLocalUtil {
    private static final TransmittableThreadLocal<Map<String, String>> CONTEXT = new TransmittableThreadLocal<>();


    public static Map<String, String> getContext() {
        Map<String, String> map = (Map)CONTEXT.get();
        if (map == null) {
            map = new HashMap(128);
            CONTEXT.set(map);
        }

        return (Map)map;
    }

    public static String getAttribute(String key) {
        return (String)getContext().get(key);
    }

    public static void setAttribute(String key, String value) {
        getContext().put(key, value);
    }

    public static void setContext(Map<String, String> context) {
        CONTEXT.set(context);
    }

    public static void removeContext() {
        CONTEXT.remove();
    }
}

